/*
 * @(#) $Header$
 *
 * Copyright (C) 2010  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.crypto.md;

import ca.forklabs.crypto.md.MDFamily;

/**
 * This class embodies the algorithm for the message digest MD5 according to
 * <a target="external" href="http://www.faqs.org/rfcs/rfc1321.html">RFC1321</a>.
 * It is more elegant to get an instance with the factory method in class
 * {@link java.security.MessageDigest} :
 * <blockquote>
 * <code>MessageDigest md5 = MessageDigest.getInstance(Forklabs.MD_5, Forklabs.NAME);</code>
 * </blockquote>
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.crypto.md.MD5">Daniel Léonard</a>
 * @version $Revision$
 * @see      ca.forklabs.crypto.Forklabs
 */
public class MD5 extends MDFamily {

//----------------------------
// Class variables
//----------------------------

   /** The name of this message digests. */
   @SuppressWarnings("nls")
   public static final String NAME = "MD5";

   /** The length in bytes of the digest. */
   public static final int DIGEST_LEN = 16;

   /** The size of the internal state in block of 32 bits. */
   public static final int STATE_LEN = 4;


//----------------------------
// Constructors
//----------------------------

   /**
    * Constructor.
    */
   public MD5() {
      super(MD5.NAME, MD5.DIGEST_LEN, MD5.STATE_LEN);
      this.coreInit();
      }


//----------------------------
// MD5 scrambling methods
//----------------------------

   private static int F(int b, int c, int d) {
      return ((b & c) | ((~b) & d));
      }

   private static int G(int b, int c, int d) {
      return ((b & d) | (c & (~d)));
      }

   private static int H(int b, int c, int d) {
      return (b ^ c ^ d);
      }

   private static int I(int b, int c, int d) {
      return (c ^ (b | (~d)));
      }


//----------------------------
// Concrete core methods
//----------------------------

   @Override
   protected void coreTransform() {
      int[] state = this.getState();
      int a = state[0];
      int b = state[1];
      int c = state[2];
      int d = state[3];

      byte[] buffer = this.getBuffer();
      int x_0  = AbstractBlockMessageDigest.makeInt(buffer, 3,  2,  1,  0);
      int x_1  = AbstractBlockMessageDigest.makeInt(buffer, 7,  6,  5,  4);
      int x_2  = AbstractBlockMessageDigest.makeInt(buffer, 11, 10, 9,  8);
      int x_3  = AbstractBlockMessageDigest.makeInt(buffer, 15, 14, 13, 12);
      int x_4  = AbstractBlockMessageDigest.makeInt(buffer, 19, 18, 17, 16);
      int x_5  = AbstractBlockMessageDigest.makeInt(buffer, 23, 22, 21, 20);
      int x_6  = AbstractBlockMessageDigest.makeInt(buffer, 27, 26, 25, 24);
      int x_7  = AbstractBlockMessageDigest.makeInt(buffer, 31, 30, 29, 28);
      int x_8  = AbstractBlockMessageDigest.makeInt(buffer, 35, 34, 33, 32);
      int x_9  = AbstractBlockMessageDigest.makeInt(buffer, 39, 38, 37, 36);
      int x_10 = AbstractBlockMessageDigest.makeInt(buffer, 43, 42, 41, 40);
      int x_11 = AbstractBlockMessageDigest.makeInt(buffer, 47, 46, 45, 44);
      int x_12 = AbstractBlockMessageDigest.makeInt(buffer, 51, 50, 49, 48);
      int x_13 = AbstractBlockMessageDigest.makeInt(buffer, 55, 54, 53, 52);
      int x_14 = AbstractBlockMessageDigest.makeInt(buffer, 59, 58, 57, 56);
      int x_15 = AbstractBlockMessageDigest.makeInt(buffer, 63, 62, 61, 60);

      AbstractBlockMessageDigest.zeroize(buffer);

   // rvalue is the expansion of the macros FF, GG, HH, II
   // round 1
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.F(b, c, d) + x_0 + 0xd76aa478,  7) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.F(a, b, c) + x_1  + 0xe8c7b756, 12) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.F(d, a, b) + x_2  + 0x242070db, 17) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.F(c, d, a) + x_3  + 0xc1bdceee, 22) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.F(b, c, d) + x_4  + 0xf57c0faf,  7) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.F(a, b, c) + x_5  + 0x4787c62a, 12) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.F(d, a, b) + x_6  + 0xa8304613, 17) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.F(c, d, a) + x_7  + 0xfd469501, 22) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.F(b, c, d) + x_8  + 0x698098d8,  7) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.F(a, b, c) + x_9  + 0x8b44f7af, 12) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.F(d, a, b) + x_10 + 0xffff5bb1, 17) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.F(c, d, a) + x_11 + 0x895cd7be, 22) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.F(b, c, d) + x_12 + 0x6b901122,  7) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.F(a, b, c) + x_13 + 0xfd987193, 12) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.F(d, a, b) + x_14 + 0xa679438e, 17) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.F(c, d, a) + x_15 + 0x49b40821, 22) + c;
   // round 2
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.G(b, c, d) + x_1  + 0xf61e2562,  5) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.G(a, b, c) + x_6  + 0xc040b340,  9) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.G(d, a, b) + x_11 + 0x265e5a51, 14) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.G(c, d, a) + x_0  + 0xe9b6c7aa, 20) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.G(b, c, d) + x_5  + 0xd62f105d,  5) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.G(a, b, c) + x_10 +  0x2441453,  9) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.G(d, a, b) + x_15 + 0xd8a1e681, 14) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.G(c, d, a) + x_4  + 0xe7d3fbc8, 20) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.G(b, c, d) + x_9  + 0x21e1cde6,  5) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.G(a, b, c) + x_14 + 0xc33707d6,  9) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.G(d, a, b) + x_3  + 0xf4d50d87, 14) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.G(c, d, a) + x_8  + 0x455a14ed, 20) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.G(b, c, d) + x_13 + 0xa9e3e905,  5) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.G(a, b, c) + x_2  + 0xfcefa3f8,  9) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.G(d, a, b) + x_7  + 0x676f02d9, 14) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.G(c, d, a) + x_12 + 0x8d2a4c8a, 20) + c;
   // round 3
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.H(b, c, d) + x_5  + 0xfffa3942,  4) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.H(a, b, c) + x_8  + 0x8771f681, 11) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.H(d, a, b) + x_11 + 0x6d9d6122, 16) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.H(c, d, a) + x_14 + 0xfde5380c, 23) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.H(b, c, d) + x_1  + 0xa4beea44,  4) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.H(a, b, c) + x_4  + 0x4bdecfa9, 11) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.H(d, a, b) + x_7  + 0xf6bb4b60, 16) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.H(c, d, a) + x_10 + 0xbebfbc70, 23) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.H(b, c, d) + x_13 + 0x289b7ec6,  4) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.H(a, b, c) + x_0  + 0xeaa127fa, 11) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.H(d, a, b) + x_3  + 0xd4ef3085, 16) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.H(c, d, a) + x_6  +  0x4881d05, 23) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.H(b, c, d) + x_9  + 0xd9d4d039,  4) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.H(a, b, c) + x_12 + 0xe6db99e5, 11) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.H(d, a, b) + x_15 + 0x1fa27cf8, 16) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.H(c, d, a) + x_2  + 0xc4ac5665, 23) + c;
   // round 4
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.I(b, c, d) + x_0  + 0xf4292244,  6) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.I(a, b, c) + x_7  + 0x432aff97, 10) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.I(d, a, b) + x_14 + 0xab9423a7, 15) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.I(c, d, a) + x_5  + 0xfc93a039, 21) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.I(b, c, d) + x_12 + 0x655b59c3,  6) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.I(a, b, c) + x_3  + 0x8f0ccc92, 10) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.I(d, a, b) + x_10 + 0xffeff47d, 15) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.I(c, d, a) + x_1  + 0x85845dd1, 21) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.I(b, c, d) + x_8  + 0x6fa87e4f,  6) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.I(a, b, c) + x_15 + 0xfe2ce6e0, 10) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.I(d, a, b) + x_6  + 0xa3014314, 15) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.I(c, d, a) + x_13 + 0x4e0811a1, 21) + c;
      a = AbstractBlockMessageDigest.rotateLeft(a + MD5.I(b, c, d) + x_4  + 0xf7537e82,  6) + b;
      d = AbstractBlockMessageDigest.rotateLeft(d + MD5.I(a, b, c) + x_11 + 0xbd3af235, 10) + a;
      c = AbstractBlockMessageDigest.rotateLeft(c + MD5.I(d, a, b) + x_2  + 0x2ad7d2bb, 15) + d;
      b = AbstractBlockMessageDigest.rotateLeft(b + MD5.I(c, d, a) + x_9  + 0xeb86d391, 21) + c;

   // add to old state
      state[0] += a;
      state[1] += b;
      state[2] += c;
      state[3] += d;
      }

   @Override
   protected void coreInit() {
      int[] state = this.getState();
      state[0] = 0x67452301;
      state[1] = 0xefcdab89;
      state[2] = 0x98badcfe;
      state[3] = 0x10325476;
      }

   }

